Skip to content

Treat a frontmatter block with no YAML content as unreadable instead of throwing - #149

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/134-null-frontmatter
Sep 27, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/134-null-frontmatter

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #134

What was wrong

YamlDotNet deserializes a YAML document with no content to null. That covers a comment only (# TODO), ~ and null. YamlSerializer.TryParseYamlObject then ran foreach over the null result. The NullReferenceException wasn't one of the caught types, so it escaped from ExtractBody, ExtractFrontmatter, RemoveFrontmatter and CombineFrontmatter.

Change

TryParseYamlObject returns false when the deserialized mapping is null. Callers already get false for an empty mapping ({}), and they now get the same for these blocks. As a result:

  • ExtractBody and RemoveFrontmatter return the body.
  • ExtractFrontmatter returns null.
  • CombineFrontmatter no longer throws.

AddFrontmatter already refuses to overwrite a block that has text in it but yields no properties, so a placeholder comment isn't silently replaced. That stays the same here, and a test now pins it for these inputs.

Tests

New file EmptyYamlBlockTests has 6 tests, and each one runs against comment-only, ~ and null blocks:

  • ExtractBody
  • ExtractFrontmatter
  • RemoveFrontmatter
  • CombineFrontmatter
  • AddFrontmatter
  • YamlSerializer.TryParseYamlObject

With YamlSerializer.cs reverted, all 18 new cases fail. With the fix, the full suite passes: 171/171.

This PR touches only YamlSerializer.cs and a new test file, so it is independent of #143, #144 and #145.

🤖 Generated with Claude Code

https://claude.ai/code/session_014jCUYfoRMwhybsUaSa1NFm


Generated by Claude Code

…instead of throwing

YamlDotNet deserializes a comment-only, `~` or `null` document to null, and
TryParseYamlObject then iterated it and threw NullReferenceException out of
ExtractBody, ExtractFrontmatter, RemoveFrontmatter and CombineFrontmatter.
It now returns false for such a block, the same as for an empty mapping.

Fixes #134

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014jCUYfoRMwhybsUaSa1NFm
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontmatter that holds only a comment or null throws NullReferenceException from ExtractBody, ExtractFrontmatter and CombineFrontmatter

2 participants